#include "statuswidget.h"

#include <QProgressBar>
#include <QGridLayout>
#include <QLabel>
#include <QDebug>

StatusWidget::StatusWidget(int shelfCapacity, int poolCapacity, QWidget *parent) :
    QWidget(parent)
{    

    QGridLayout* grid = new QGridLayout(this);

    shelfFullness_ = new QProgressBar;
    initProgressBar(shelfFullness_);
    shelfFullness_->setMaximum(shelfCapacity);
    grid->addWidget(shelfFullness_, 1, 1, Qt::AlignCenter);
    grid->addWidget(new QLabel("Written books"), 2, 1, Qt::AlignCenter);

    activeUsers_ = new QProgressBar;
    initProgressBar(activeUsers_);
    activeUsers_->setMaximum(poolCapacity);
    grid->addWidget(activeUsers_, 1, 2, Qt::AlignCenter);
    grid->addWidget(new QLabel("Active users"), 2, 2, Qt::AlignCenter);

    waitingWriters_ = new QProgressBar;
    initProgressBar(waitingWriters_);
    waitingWriters_->setMaximum(poolCapacity);
    grid->addWidget(waitingWriters_, 1, 3, Qt::AlignCenter);
    grid->addWidget(new QLabel("Waiting writers"), 2, 3, Qt::AlignCenter);

    setLayout(grid);
}

void StatusWidget::incBook()
{
    shelfFullness_->setValue(shelfFullness_->value() + 1);
}

void StatusWidget::initProgressBar(QProgressBar* bar) {
    bar->setAlignment(Qt::AlignCenter);
    bar->setMinimum(0);
    bar->setMaximum(100);
    bar->setOrientation(Qt::Vertical);
    bar->setValue(0);
}

void StatusWidget::incWaiting()
{
    waitingWriters_->setValue(waitingWriters_->value() + 1);
    activeUsers_->setValue(activeUsers_->maximum() - waitingWriters_->value());
}

void StatusWidget::decWaiting()
{
    waitingWriters_->setValue(waitingWriters_->value() - 1);
    activeUsers_->setValue(activeUsers_->maximum() - waitingWriters_->value());
}
